<template>
	<jk-dialog
		v-model="showDialog"
		title="跳过本次保养"
		width="800px"
		:show-content-loading="showLoading"
		:confirm-btn-loading="saveBtnLoading"
		@on-cancel="onVisibleChange(false)"
		@on-visible-change="onVisibleChange"
		@on-confirm="onConfirmBtn"
	>
		<div ref="box1">
			<el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="120px" :show-message="false">
				<el-tag class="margin-bottom-4" type="primary" effect="plain">
					<span class="el-icon-tickets item-gap"></span>
					<span>基本信息</span>
				</el-tag>
				<jk-card class="margin-bottom-20">
					<el-row class="flex-start-center-wrap">
						<el-col :span="12">
							<el-form-item class="margin-bottom-10" label="任务单号">
								<span class="read-only-2">{{ ruleForm.code }}</span>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item class="margin-bottom-10" label="设备">
								<span class="read-only-2">{{ ruleForm.machineCode }}</span>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item class="margin-bottom-10" label="项目">
								<span class="read-only-2">{{ ruleForm.upkeepItemName }}</span>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item class="margin-bottom-10" label="周期">
								<span class="read-only-2">{{ ruleForm.cycleUnitName }}</span>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item class="margin-bottom-0" label="上次保养时间">
								<span class="read-only-2">{{ ruleForm.lastTime || '无' }}</span>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item class="margin-bottom-0" label="计划保养时间">
								<span class="read-only-2">{{ ruleForm.planUpkeepTime }}</span>
							</el-form-item>
						</el-col>
					</el-row>
				</jk-card>
				<el-tag class="margin-bottom-4" type="primary" effect="plain">
					<span class="el-icon-document-checked item-gap"></span>
					<span>计划下次</span>
				</el-tag>
				<jk-card class="">
					<el-row>
						<el-col :span="12">
							<el-form-item class="margin-bottom-0" label="计划下次时间" prop="nextPlanTime">
								<jk-date-picker v-model="ruleForm.nextPlanTime" size="medium" type="datetime" placeholder="计划下次时间"></jk-date-picker>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item class="margin-bottom-0" label="下次保养人">
								<remote-search-emp-input
									size="medium"
									placeholder="下次保养人"
									only-key="nextPlanUpkeepEmpId"
									query-name="nextPlanUpkeepEmpName"
									:form-data="ruleForm"
									@on-select="onSelectNextEmpChange"
								></remote-search-emp-input>
							</el-form-item>
						</el-col>
					</el-row>
				</jk-card>
			</el-form>
		</div>
	</jk-dialog>
</template>

<script>
    import calHeight from '/src/mixins/cal-dialog-height';
    import remoteSearchEmpInput from '../../../production/order/components/remote-search-emp-input';
    import { clearRedundancyKey, formatTime } from '../../../../utils/jk/jk-tool';
    import {
        machineManagerUpkeepMachineJobPass,
        machineManagerUpkeepMachineJobGet
    } from '../../../../api/machine/upkeepTask';
    import upkeepMixin from './upkeep';
    export default {
        mixins: [calHeight, upkeepMixin],
        components: {
            remoteSearchEmpInput
        },
        props: {
            dialogState: {
                type: Boolean,
                default: false
            },
            dataId: {
                type: [String, Number],
                default: ''
            }
        },
        data() {
            return {
                showDialog: false,
                showLoading: true,
                saveBtnLoading: false,
                ruleForm: {
                    id: null,
                    code: '',
                    workshopId: null,
                    nextPlanTime: '',
                    nextPlanUpkeepEmpId: '',
                    actualUpkeepDescription: '',
                    actualUpkeepEmpId: '',
                    actualUpkeepEmpName: '',
                    actualStartTime: '',
                    actualEndTime: ''
                },
                rules: {
                    nextPlanTime: [{ required: true, trigger: 'change' }],
                    nextPlanUpkeepEmpId: [{ required: true, trigger: 'change' }]
                }
            };
        },
        methods: {
            onSelectEmpChange(e) {
                if (Object.keys(e).length) {
                    this.$set(this.ruleForm, 'actualUpkeepEmpId', e.id);
                    this.$set(this.ruleForm, 'actualUpkeepEmpName', e.name);
                    this.$set(this.ruleForm, 'nextPlanUpkeepEmpId', e.id);
                    this.$set(this.ruleForm, 'nextPlanUpkeepEmpName', e.name);
                }
            },
            onSelectNextEmpChange(e) {
                if (Object.keys(e).length) {
                    this.$set(this.ruleForm, 'nextPlanUpkeepEmpId', e.id);
                    this.$set(this.ruleForm, 'nextPlanUpkeepEmpName', e.name);
                }
            },
            getParamData() {
                const params = {
                    id: this.ruleForm.id,
                    nextPlanTime: this.ruleForm.nextPlanTime,
                    nextPlanUpkeepEmpId: this.ruleForm.nextPlanUpkeepEmpId,
                    nextPlanUpkeepEmpName: this.ruleForm.nextPlanUpkeepEmpName
                };
                return params;
            },
            createAndUpdateRequest() {
                this.saveBtnLoading = true;
                machineManagerUpkeepMachineJobPass(this.getParamData()).then(res => {
                    this.saveBtnLoading = false;
                    if (res.data.status === 200) {
                        this.$tipsBar(this, 'save');
                        this.$emit('on-finish', res.data.res);
                    }
                });
            },
            onConfirmBtn() {
                this.$refs['ruleForm'].validate(valid => {
                    if (valid) {
                        this.createAndUpdateRequest();
                    } else {
                        this.$tipsBar(this, 'validator');
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                    this.$nextTick(() => {
                        this.$refs.ruleForm.resetFields();
                    });
                }
            },
            getDetailRequest() {
                return machineManagerUpkeepMachineJobGet({ id: this.dataId }).then(res => {
                    if (res.data.status === 200) {
                        this.ruleForm = clearRedundancyKey(res.data.res);
                        this.ruleForm.cycleUnitName = this.transferCircleUnit(clearRedundancyKey(res.data.res));
                        this.$nextTick(() => {
                            this.$refs.ruleForm.clearValidate();
                        });
                    }
                });
            },
            async getDependentData() {
                this.showLoading = true;
                if (this.dataId) await this.getDetailRequest();
                this.$set(this.ruleForm, 'nextPlanTime', formatTime(Date.now()));
                this.$set(this.ruleForm, 'nextPlanUpkeepEmpId', this.$store.state.user.empInfo.id);
                this.$set(this.ruleForm, 'nextPlanUpkeepEmpName', this.$store.state.user.empInfo.name);
                this.showLoading = false;
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.getDependentData();
                }
            }
        }
    };
</script>
<style scoped lang="scss">

</style>
